package com.jr9z.jrjz.controller;

import com.jr9z.jrjz.common.BaseResponse;
import com.jr9z.jrjz.common.ResponseBuilder;
import com.jr9z.jrjz.entity.Credentials;
import com.jr9z.jrjz.service.CredentialsService;
import com.jr9z.jrjz.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author:Mrzhang
 * @Description:
 * @Date:2019.05.14 22:42
 **/

@Controller
@RequestMapping("/credential")
public class CredentialsController {

    @Autowired
    private CredentialsService credentialsService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 跳转到荣誉资质上传页面
     *
     * @return
     */
    @RequestMapping("/credentialManage")
    public String classicCaseManage() {

        logger.info("进入荣誉资质管理");
        return "credentials/credentialAdd";
    }

    /**
     * 跳转到经典案例管理页面
     *
     * @return
     */
    @RequestMapping("/credentialList")
    public String classicCaseList() {

        logger.info("进入荣誉资质管理");
        return "credentials/credentialManage";
    }


    /**
     * 获取全部荣誉资质证书
     *
     * @return
     */
    @RequestMapping(value = "/getCredentials", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getCredentialList() {

        ResponseBuilder custom = ResponseBuilder.custom();

        List<Credentials> credentialsList;

        try {

            credentialsList = credentialsService.getCredentialList();


            if (credentialsList != null) {
                custom.data(credentialsList).success(Constant.SUCCESS, Constant.SUCCESS_CODE);

            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }


    /**
     * 上传荣誉证书
     *
     * @param credentials
     * @return
     */
    @RequestMapping(value = "/admin/addCredential", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addCredential(@RequestBody Credentials credentials) {
        ResponseBuilder custom = ResponseBuilder.custom();

        int result = 0;
        try {

            result = credentialsService.addCredential(credentials);

            Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 根据id删除荣誉证书
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/admin/deleteCredential", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse deleteCredential(Integer id) {
        ResponseBuilder custom = ResponseBuilder.custom();

        int result = 0;
        try {

            result = credentialsService.deleteCredential(id);

            Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }
}
